package main

import (
	"github.com/gin-gonic/gin"
	"gopkg.in/ini.v1"
	"luna/room"
	"luna/server"
	"luna/util"
	"luna/websockets"
	"os"
)

func main() {

	//加载配置文件
	cfg, err := ini.Load("configs/config.ini")
	//加载出错,打印错误信息
	if err != nil {
		util.Errorf("读取文件失败: %v", err)
		os.Exit(1)
	}

	//读取证书Cert配置
	sslCert := cfg.Section("general").Key("cert").String()
	//读取证书Key配置
	sslKey := cfg.Section("general").Key("key").String()

	r := gin.Default()
	r.GET("/ping", func(c *gin.Context) {
		c.JSON(200, gin.H{
			"message": "pong",
		})
	})
	//测试webrtc发送跟接受
	r.GET("/websocket", websockets.HandleDoing)

	//实例化房间管理
	roomManager := room.NewRoomManager()
	//创建一个SFU服务
	wsServer := server.NewSFUServer(roomManager.HandleNewWebSocket)

	//实例化SFUServerConfig对象
	config := server.DefaultConfig()
	//主机地址
	config.Host = "0.0.0.0"
	//端口
	config.Port = 3000
	//Cert文件
	config.CertFile = sslCert
	//Key文件
	config.KeyFile = sslKey
	//Html根路径
	config.HTMLRoot = "html"
	//绑定配置
	wsServer.Bind(config, r)
}
